package common;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.naming.NamingException;
import javax.servlet.http.*;

public class agent_contents_link {
    public static void main(String[] args) throws Exception{
        Connection con          = null;
        CallableStatement call  = null;
	CallableStatement real_call = null;
	CallableStatement link_call = null;
        ResultSet rs            = null;

        String dirName_Real="./tmp_real/";
	String dirName_Link="./tmp_link/";

        String course_cd="";
	String course_gauge_stat="";

        File  real_file = null;
	File  link_file = null;

            try {
                Class.forName("com.mysql.jdbc.Driver");
                if (args[0].toString().equals("test")) {
                	con = DriverManager.getConnection ("jdbc:mysql://10.10.20.143:3306/lms","eduhrd_lms_site","site5508448");
                } else {
                	con = DriverManager.getConnection ("jdbc:mysql://192.168.2.27:9110/lms","eduhrd_lms_site","site5508448");
                }


                call = con.prepareCall("{call CourseInfo()}");

                call.execute();
                rs = call.getResultSet();

                while (rs.next()) {
                    course_cd    = rs.getString(1);
                    course_gauge_stat = rs.getString(2);

                    real_file = new File(dirName_Real + course_cd.toString());

                    if (!real_file.isDirectory()) {
                        real_file.mkdirs();
                    }
                    real_call = con.prepareCall("{call CourseInfo_real_update(?)}");
                    real_call.setString(1, course_cd);
                    real_call.execute();

		    link_file = new File(dirName_Link + course_cd.toString());

		    if (course_gauge_stat.equalsIgnoreCase("Y") ) {
			if(link_file.isDirectory()){
				link_file.delete();
			}
                        link_call = con.prepareCall("{call CourseInfo_link_update(?,?)}");
                        link_call.setString(1, course_cd);
                        link_call.setString(2, "N");
                        link_call.execute();

		    }else{
		    	if (!link_file.isDirectory()) {
				link_file.mkdirs();
		    	}
                        link_call = con.prepareCall("{call CourseInfo_link_update(?,?)}");
                        link_call.setString(1, course_cd);
                        link_call.setString(2, "Y");
                        link_call.execute();

		    }
                }

				String inDate   = new java.text.SimpleDateFormat("yyyyMMdd").format(new java.util.Date());
            	String inTime   = new java.text.SimpleDateFormat("HHmmss").format(new java.util.Date());
                System.out.println("agent_contents_link excuted at " + inDate + inTime);

            } catch (SQLException e) {
                e.printStackTrace();
//            } catch (ClassNotFoundException e) {
//               e.printStackTrace();
            }finally{
                con.close();
                con.close();
            }
    }
}

